package com.dfqy.controller;

import com.dfqy.pojo.Sym;
import com.dfqy.pojo.SymType;
import com.dfqy.service.SymService;
import com.dfqy.service.SymTypeService;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sym")
@Api("症状管理")
public class SymController {

    @Autowired
    private SymService symService;

    @RequestMapping(value = "allsym")
    public Page<Sym> paging(@RequestParam(defaultValue = "1") int page, int limit, String sym, String poster){
        Page<Sym> symPage = symService.paging(page,limit,sym,poster);
        return symPage;
    }

    @PostMapping(value = "/addsym",produces = "application/json;charset=UTF-8")
    public Result add(@RequestBody Sym sym){

        int r = symService.add(sym);
        if (r > 0)
            return Result.success();
        return Result.error("新增失败");
    }

    @RequestMapping(value = "/findsym/{id}",produces = "application/json;charset=UTF-8")
    public Result findSym(@PathVariable long id){
        System.out.println("要修改的id是："+id);
        Sym sym = symService.getInfoById(id);
        return sym == null ? Result.error("该管理员不存在") : Result.success(sym);
    }

    @RequestMapping(value = "/updatesym",produces = "application/json;charset=UTF-8")
    public Result update(@RequestBody Sym sym){

        int r = symService.update(sym);
        if (r > 0)
            return Result.success();
        return Result.error("修改失败");
    }

    @RequestMapping(value = "/delsym",produces = "application/json;charset=UTF-8")
    public Result del(String id){
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int r = symService.delete(ids);
        return r > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    //修改启用状态通过滑动按钮
    @RequestMapping(value = "/changeIsValid",produces = "application/json;charset=UTF-8")
    public Result changeIsValid(long id,byte isvalid){
        isvalid = (byte) (isvalid>0?1:0);
        Sym byId = symService.getInfoById(id);
        byId.setIsValid(isvalid);
        int r = symService.update(byId);
        if (r > 0)
            return Result.success();
        return Result.error("启/禁用失败");
    }

    //增加时前端症状类型验证
    @RequestMapping(value = "/addchecksym",produces = "application/json;charset=UTF-8")
    public Result addCheckTitle(String sym){
        boolean r = symService.addchecktitle(sym);
        return r ? Result.error("症状已经存在") : Result.success();
    }

    //修改时前端症状类型验证
    @RequestMapping(value = "/checksymtype",produces = "application/json;charset=UTF-8")
    public Result checkTitle(long id,String sym){
        boolean r = symService.checktitle(sym,id);
        return r ? Result.error("症状已经存在") : Result.success();
    }
}
